package com.dadibadi.core.member.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dadibadi.core.common.exception.MyBizException;
import com.dadibadi.core.dao.entity.MemberApplyTab;
import com.dadibadi.core.dao.entity.SysRoleTab;
import com.dadibadi.core.dao.entity.UserInfoTab;
import com.dadibadi.core.dao.mapper.MemberApplyMapper;
import com.dadibadi.core.dao.mapper.SysRoleMapper;
import com.dadibadi.core.member.qo.MemberApplyEditQO;
import com.dadibadi.core.member.qo.MemberApplyPageQO;
import com.dadibadi.core.member.vo.MemberApplyListVO;
import com.dadibadi.core.user.service.UserInfoService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MemberService {
    @Resource
    MemberApplyMapper memberApplyMapper;

    @Resource
    UserInfoService userInfoService;

    @Resource
    SysRoleMapper sysRoleMapper;

    /**
     * 分页查找正在申请的会员
     *
     * @return
     */
    public MemberApplyTab saveMemberApply(MemberApplyEditQO qo) {
        MPJLambdaWrapper<MemberApplyTab>wrapper=new MPJLambdaWrapper<MemberApplyTab>()
                .selectAll(MemberApplyTab.class)
                .leftJoin(UserInfoTab.class,UserInfoTab::getUserId,MemberApplyTab::getUserId);
        MemberApplyTab memberApplyTab = memberApplyMapper.selectJoinOne(MemberApplyTab.class,wrapper);
        if (memberApplyTab==null) {
            memberApplyTab = new MemberApplyTab();
            BeanUtils.copyProperties(qo, memberApplyTab);
            memberApplyMapper.insert(memberApplyTab);
        } else {
            memberApplyTab = memberApplyMapper.selectById(qo.getUserId());
            BeanUtils.copyProperties(qo, memberApplyTab);
            memberApplyMapper.updateById(memberApplyTab);
        }
        return memberApplyTab;

    }

    public IPage<MemberApplyListVO> pageApplyingMember(MemberApplyPageQO qo) {
        MPJLambdaWrapper<MemberApplyTab> wrapper = new MPJLambdaWrapper<MemberApplyTab>()
                .selectAll(MemberApplyTab.class);
        IPage<MemberApplyListVO> result = memberApplyMapper.selectJoinPage(qo.getPage(), MemberApplyListVO.class, wrapper);
        return result;
    }

    public MemberApplyEditQO permitMemberApply(MemberApplyEditQO qo) {
        MemberApplyTab memberApplyTab = new MemberApplyTab();
        memberApplyTab = memberApplyMapper.selectById(qo.getUserId());
        if (memberApplyTab == null) {
            throw MyBizException.NOT_APPLY;
        }
        memberApplyTab.setMemberFlag(1);
        MemberApplyEditQO memberApplyEditQO = new MemberApplyEditQO();
        BeanUtils.copyProperties(memberApplyTab, memberApplyEditQO);
        saveMemberApply(memberApplyEditQO);
        SysRoleTab sysRoleTab = new SysRoleTab();
        sysRoleTab.setUserId(qo.getUserId());
        sysRoleTab.setRoleType("MEMBER");
        sysRoleMapper.updateById(sysRoleTab);
        return memberApplyEditQO;
    }


}
